/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author theworldiwant
 */
public class Gui2 extends JPanel implements MouseListener{
    private int nVal, pVal;
    private Object[][] finalTable;
    private ArrayList<Object[][]> roundTable1, roundTable2;
    private JFrame mainFrame, frame;
    private JTable tableR1, tableR2, tableF;
    private int roundNum;                                         //left pic, rightpics
    private JTextArea jTa;
    private BufferedImage back, bg;
    private BufferedImage left1, left2, right1, right2;
    private Font font2, font3;
    private int[] deckSize;
    
    public Gui2(Object[][] finalTable, int pVal, int  nVal, ArrayList<Object[][]>  roundTable, JFrame mainFrame, int[] deck){
        this.finalTable=finalTable;
        this.pVal=pVal;
        this.nVal=nVal;
        this.deckSize=deck;
        split(roundTable);
        this.mainFrame=mainFrame;
        init();
    }
    
    public void split(ArrayList<Object[][]> roundTable){
        roundTable1=new ArrayList<Object[][]> ();
        roundTable2=new ArrayList<Object[][]> ();
        
        Object[][] temp = new Object[4][3];
        Object[][] temp2 = new Object[4][3];
        
        for(int i=0; i<roundTable.size(); i++){
            for(int j=0; j<4; j++){
                temp[j][0]=(Integer)(roundTable.get(i)[j][0])+1;
                temp[j][1]=roundTable.get(i)[j][1];
                temp[j][2]=roundTable.get(i)[j][2];
                temp2[j][0]=(Integer)roundTable.get(i)[j][0]+1;
                temp2[j][1]=roundTable.get(i)[j][4];
                temp2[j][2]=roundTable.get(i)[j][5];
             }
             roundTable1.add(temp);
             roundTable2.add(temp2);
             temp = new Object[4][3];
             temp2 = new Object[4][3];
           
        }
        
    }
    public void init(){
      
            roundNum=0;
                    
             frame = new JFrame("Lucky 9!");
             frame.setContentPane(this);
            JSeparator js1 = new JSeparator(JSeparator.VERTICAL);
            JSeparator js2 = new JSeparator(JSeparator.HORIZONTAL);
            JButton jb1 = new JButton("<");
            JButton jb2 = new JButton(">");
            JLabel jL1 = new JLabel("Round");
            JLabel jL2 = new JLabel("N = "+nVal);
            JLabel jL3 = new JLabel("p = "+pVal);
            
             jTa = new JTextArea(""+(roundNum+1));
            Font font = new Font("Calibri", Font.PLAIN, 16);
            font2 = new Font("Calibri", Font.PLAIN, 20);
            font3 = new Font("Calibri", Font.PLAIN, 14);
            
            Object[] str= {"Order", "Player", "Score"};
            Object[] str2=  {"Order", "AskForThird", "Rank"};
         
          
            tableR1 = new JTable( new DefaultTableModel(roundTable1.get(0), str));
            tableR2= new JTable( new DefaultTableModel(roundTable2.get(0),  str2));
            tableF = new JTable(finalTable, new Object[]{"Player", "1st", "2nd", "3rd", "4th"});
            tableR1.getColumnModel().getColumn(0).setPreferredWidth(20);
            tableR1.getColumnModel().getColumn(1).setPreferredWidth(45);
            tableR2.getColumnModel().getColumn(0).setPreferredWidth(20);
            tableR2.getColumnModel().getColumn(1).setPreferredWidth(95);
            tableR2.getColumnModel().getColumn(2).setPreferredWidth(25);
             
           // tableR1.setDragEnabled(false);
            
            
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(750, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.addWindowListener(
                    new WindowListener(){
                @Override
                public void windowOpened(WindowEvent we) {}

                @Override
                public void windowClosing(WindowEvent we) {}

                @Override
                public void windowClosed(WindowEvent we) {
                        mainFrame.setVisible(true);
                }

                @Override
                public void windowIconified(WindowEvent we) {}

                @Override
                public void windowDeiconified(WindowEvent we) {}

                @Override
                public void windowActivated(WindowEvent we) {}

                @Override
                public void windowDeactivated(WindowEvent we) {}
                    }
              );
            
            frame.getContentPane().setLayout(null);
            frame.getContentPane().setBackground(new java.awt.Color(238, 238, 238));
            this.addMouseListener(this);
            
            js1.setForeground(new java.awt.Color(204, 204, 204)); 
            js2.setForeground(new java.awt.Color(204, 204, 204)); 
            jL1.setFont(font);
            jTa.setFont(font);
            jL2.setFont(font);
            jL3.setFont(font);
            jL1.setBackground(Color.BLACK);
           // tableR1.set
            tableR1.setEnabled(false);
            tableR2.setEnabled(false);
            tableF.setEnabled(false);
            
            JScrollPane sp = new JScrollPane(tableR1);
            JScrollPane sp2 = new JScrollPane(tableR2);
            JScrollPane sp3 = new JScrollPane(tableF);
         
            js1.setBounds(500, 0, 10, 325);
            js2.setBounds(0, 320, 750, 10);
            jb1.setBounds(510, 15, 65, 29);
            jb2.setBounds(680, 15, 65, 29);
            jL1.setBounds(583, 18, 50, 20);
         
            jL2.setBounds(35, 340, 100, 20);
            jL3.setBounds(200, 340, 50, 20);
            jTa.setBounds(635, 18, 40, 20);
            sp.setBounds(511, 75,235,90);
            sp2.setBounds(511, 195,235,90);
            sp3.setBounds(25, 375, 590, 90);
            
            frame.getContentPane().add(js1);
            frame.getContentPane().add(js2);
            frame.getContentPane().add(jb1);
            frame.getContentPane().add(jb2);
            frame.getContentPane().add(jL1);
            frame.getContentPane().add(jL2);
            frame.getContentPane().add(jL3);
            frame.getContentPane().add(jTa);
            frame.getContentPane().add(sp);
            frame.getContentPane().add(sp2);
            frame.getContentPane().add(sp3);
            
            
            jTa.addKeyListener(
                   new KeyListener(){

                @Override
                public void keyTyped(KeyEvent ke) { }

                @Override
                public void keyPressed(KeyEvent ke) {
                    
                        if(ke.getKeyChar()==KeyEvent.VK_ENTER){
                           int oldNum=roundNum;                                             //if any input is not acceptable
                            try{
                                int x=Integer.parseInt(jTa.getText());
                                if(x>nVal){                                                                 //inputs greater than nVal
                                    x=nVal;
                                    jTa.setText((x+1)+"");
                                }
                                else if(x<0){                                                               //negative inputs
                                   x=0;
                                   jTa.setText("1");
                                }
                                roundNum=x-1;
                            }catch(Exception e){
                                roundNum=oldNum;
                            }
                            jTa.setText((roundNum+1)+"");
                            populateRoundTable();
                            repaint();
                        }
                }

                @Override
                public void keyReleased(KeyEvent ke) {
                } });
            
           jb1.addActionListener(
                    new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent ae) {
                      if(roundNum==0 ){
                            roundNum = nVal-1;
                        }
                     else{
                        roundNum--;
                     }
                    jTa.setText(""+(roundNum+1));
                    populateRoundTable();
                    repaint();
                }
                 });
           
            jb2.addActionListener(
                    new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent ae) {
                    
                     if(roundNum+1== nVal){                                                         //loops
                        roundNum = 0;
                    }
                    else{
                        roundNum++;
                    }
                    jTa.setText(""+(roundNum+1));
                    populateRoundTable();
                  repaint();
               }   
        });
        
       try {
            back= ImageIO.read(new File("images/home.png"));
            bg= ImageIO.read(new File("images/main.png"));
            left1=ImageIO.read(new File("images/players/S2.png"));
            left2=ImageIO.read(new File("images/players/A2.png"));
            right1=ImageIO.read(new File("images/players/R1.png"));
            right2=ImageIO.read(new File("images/players/I1.png"));
        } catch (IOException ex) {
            Logger.getLogger(Gui2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2D=(Graphics2D)g;
        
        g2D.drawImage(back, 640, 375, null);
        g2D.drawImage(bg, 0, 0, null);
        g2D.drawImage(left1, 50, 110, null);
        g2D.drawImage(left2, 50, 220, null);
        g2D.drawImage(right1, 370, 110, null);
        g2D.drawImage(right2, 370, 220, null);
        
        for(int i=0; i<4; i++){
           g2D.setFont(font2);
            g2D.setColor(Color.BLACK);
            refreshPic(g2D, i);
        }
        g2D.setFont(font3);
        g2D.setColor(Color.white);
        g2D.drawString("Deck Size: ", 225, 130);
        g2D.setColor(new Color(255, 247, 143));
        g2D.drawString(deckSize[roundNum]+"", 245, 145);
       
    }
    public void refreshPic(Graphics2D g2D, int j){
        int n=(Integer)roundTable1.get(roundNum)[j][0];
        String str=roundTable1.get(roundNum)[j][1]+"";
        String rank=roundTable2.get(roundNum)[j][2]+"";
        String score=roundTable2.get(roundNum)[j][1]+"";
        
     
        if(rank.equals("1"))      rank="1st";
        if(rank.equals("2"))      rank="2nd";
        if(rank.equals("3"))      rank="3rd";
        if(rank.equals("4"))      rank="4th";
          if(j==0){
                g2D.drawString((n)+"", 88, 160);
                g2D.setColor(Color.WHITE);
                g2D.setFont(font3);
                g2D.drawString(str, 58, 205);
                g2D.drawString(score, 160, 180);
                g2D.setFont(font2);
                g2D.setColor(new Color(255, 247, 143));
                g2D.drawString(rank, 18, 160);
                
                
            }
          
          if(j==1){
                g2D.drawString((n)+"", 90, 270);
                g2D.setColor(Color.WHITE);
                g2D.setFont(font3);
                g2D.drawString(str, 58, 318);  
                g2D.drawString(score, 160, 290);
                g2D.setFont(font2);
                g2D.setColor(new Color(255, 247, 143));
                g2D.drawString(rank, 18, 270);
            }   
          if(j==2){
                g2D.drawString((n)+"", 408, 160);
                g2D.setColor(Color.WHITE);
                g2D.setFont(font3);
                g2D.drawString(str, 378, 205);
                g2D.drawString(score, 270, 180);
                g2D.setFont(font2);
                g2D.setColor(new Color(255, 247, 143));
                g2D.drawString(rank, 465, 160);
            }
              if(j==3){
               g2D.drawString((n)+"", 408, 270);  
                g2D.setColor(Color.WHITE);
                g2D.setFont(font3);
                g2D.drawString(str, 378, 318);
                g2D.drawString(score, 270, 290);
                g2D.setFont(font2);
                g2D.setColor(new Color(255, 247, 143));
                g2D.drawString(rank, 465, 270);
            } 
    }
    
    public void populateRoundTable(){                                                           //refresh table
        
        Object[][] roundTemp = roundTable1.get(roundNum);
        Object[][] roundTemp2 = roundTable2.get(roundNum);
        for(int j =0; j<4; j++){
        tableR1.getModel().setValueAt(roundTemp[j][0] , j, 0);
        tableR1.getModel().setValueAt(roundTemp[j][1] , j, 1);
        tableR1.getModel().setValueAt(roundTemp[j][2] , j, 2);
        tableR2.getModel().setValueAt(roundTemp2[j][0] , j, 0);
        tableR2.getModel().setValueAt(roundTemp2[j][1] , j, 1);
        tableR2.getModel().setValueAt(roundTemp2[j][2] , j, 2); 
        }
        
     }

    @Override
    public void mouseClicked(MouseEvent me) {
        if(me.getX()>=637&& me.getX()<720&& me.getY()>=375&&me.getY()<451)
            frame.dispose();
        //507, 326
    }

    @Override
    public void mousePressed(MouseEvent me) {}

    @Override
    public void mouseReleased(MouseEvent me) {}

    @Override
    public void mouseEntered(MouseEvent me) {}

    @Override
    public void mouseExited(MouseEvent me) {}
}
